using Domain.Entities;
using NHibernate;
using NHibernate.Expression;
using System.Collections.Generic;

namespace Domain.Services
{
    public static class GroupService
    {
        /*****************************************************************************************
        * Getters
        * ***************************************************************************************/
        public static IList<Group> GetAll()
        {
            ISession session = NHibernateHelper.Instance.CurrentSession;
            return session.CreateCriteria(typeof(Group)).AddOrder(Order.Asc("Name")).List<Group>();
        }

        public static Group GetById(short groupId)
        {
            ISession session = NHibernateHelper.Instance.CurrentSession;
            return session.Get<Group>(groupId);
        }

        /*****************************************************************************************
        * Adders
        * ***************************************************************************************/
        public static Group NewGroup
        (
            string name, 
            string prefix, 
            string suffix, 
            bool canViewActiveUserlist, 
            bool canViewUserlist, 
            bool canViewProfiles,
            bool canUseSearch,
            bool isAdministratorGroup
        )
        {
            ISession session = NHibernateHelper.Instance.CurrentSession;
            using (ITransaction transaction = session.BeginTransaction())
            {
                Group group = new Group();
                group.Name = name;
                group.Prefix = prefix;
                group.Suffix = suffix;
                group.CanViewActiveUserlist = canViewActiveUserlist;
                group.CanViewUserlist = canViewUserlist;
                group.CanViewProfiles = canViewProfiles;
                group.CanUseSearch = canUseSearch;
                group.IsAdministratorGroup = isAdministratorGroup;
                session.SaveOrUpdate(group);

                transaction.Commit();

                return group;
            }
        }

        /*****************************************************************************************
        * Updaters
        * ***************************************************************************************/
        public static void UpdateGroup
        (
            Group group,
            string name,
            string prefix,
            string suffix,
            bool canViewActiveUserlist,
            bool canViewUserlist,
            bool canViewProfiles,
            bool canUseSearch,
            bool isAdministratorGroup
        )
        {
            ISession session = NHibernateHelper.Instance.CurrentSession;
            using (ITransaction transaction = session.BeginTransaction())
            {
                group.Name = name;
                group.Prefix = prefix;
                group.Suffix = suffix;
                group.CanViewActiveUserlist = canViewActiveUserlist;
                group.CanViewUserlist = canViewUserlist;
                group.CanViewProfiles = canViewProfiles;
                group.CanUseSearch = canUseSearch;
                group.IsAdministratorGroup = isAdministratorGroup;
                session.SaveOrUpdate(group);

                transaction.Commit();
            }
        }

        /*****************************************************************************************
        * Deleters
        * ***************************************************************************************/
        public static void RemoveGroup(Group group)
        {
            // TODO: implement
        }
    }
}
